import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_loggy/flutter_loggy.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:loggy/loggy.dart';
import 'package:tushare_flutter/app/config/custom_theme_data.dart';
import 'package:tushare_flutter/app/config/route_config.dart';
import 'package:tushare_flutter/http/net/http_client.dart';
import 'package:tushare_flutter/http/net/http_config.dart';

void main() async {
  await GetStorage.init();

  HttpConfig dioConfig = HttpConfig(baseUrl: "http://api.biyingapi.com");
  // HttpConfig(baseUrl: "https://gank.io/", proxy: "192.168.2.249:8888");
  HttpClient client = HttpClient(dioConfig: dioConfig);
  Get.put<HttpClient>(client);

  Loggy.initLoggy(logPrinter: const PrettyDeveloperPrinter());

  _loadMyApp();
}

void _loadMyApp() {
  runApp(const MyApp());
  // 透明状态栏
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Brightness.dark,
      statusBarBrightness: Brightness.dark,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<StatefulWidget> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      initialRoute: RouteConfig.index,
      getPages: RouteConfig.getPages,
      title: '猞先生',
      themeMode: ThemeMode.system,
      theme: ThemeData(
        useMaterial3: true,
        focusColor: const Color(0xFF333333),
        primaryColor: Colors.lightGreen,
        dividerTheme: const DividerThemeData(color: Color(0xFFE5E5E5), thickness: 0),
        scaffoldBackgroundColor: Colors.white,
        appBarTheme: const AppBarTheme(
          titleTextStyle: TextStyle(color: Color(0xFF333333), fontSize: 18),
          backgroundColor: Color(0xfff2f3f6),
          foregroundColor: Color(0xFF333333),
          centerTitle: true,
          titleSpacing: 0,
          elevation: 0,
          systemOverlayStyle: SystemUiOverlayStyle(systemNavigationBarColor: Colors.white, statusBarBrightness: Brightness.dark),
        ),
        cardTheme: const CardTheme(color: Colors.white, elevation: 0),
        textTheme: const TextTheme(
          titleSmall: TextStyle(color: Color(0xFF333333), fontSize: 12, fontWeight: FontWeight.w300),
          titleMedium: TextStyle(color: Color(0xFF333333), fontSize: 15, fontWeight: FontWeight.w500), // TextFiled 默认文字样式
          titleLarge: TextStyle(color: Color(0xFF333333), fontSize: 18, fontWeight: FontWeight.bold), // AppBar默认文字样式
          bodySmall: TextStyle(color: Color(0xFF666666), fontSize: 12, fontWeight: FontWeight.w300),
          bodyMedium: TextStyle(color: Color(0xFF666666), fontSize: 15, fontWeight: FontWeight.w500), // Text默认文字样式
          bodyLarge: TextStyle(color: Color(0xFF666666), fontSize: 18, fontWeight: FontWeight.bold),
          displaySmall: TextStyle(color: Color(0xFF999999), fontSize: 12, fontWeight: FontWeight.w300),
          displayMedium: TextStyle(color: Color(0xFF999999), fontSize: 15, fontWeight: FontWeight.w500),
          displayLarge: TextStyle(color: Color(0xFF999999), fontSize: 18, fontWeight: FontWeight.bold), // Button child Text默认文字样式
        ),
        colorScheme: lightColorScheme,
      ),
      darkTheme: ThemeData(
        useMaterial3: true,
        focusColor: Colors.white,
        primaryColor: Colors.lightGreen,
        dividerTheme: const DividerThemeData(color: Color(0xFF252525), thickness: 0),
        scaffoldBackgroundColor: const Color(0xFF111111),
        appBarTheme: AppBarTheme(
          titleTextStyle: const TextStyle(color: Colors.white, fontSize: 18),
          backgroundColor: const Color(0xFF111111),
          foregroundColor: Colors.white.withOpacity(0.9),
          centerTitle: true,
          titleSpacing: 0,
          elevation: 0,
          systemOverlayStyle: const SystemUiOverlayStyle(systemNavigationBarColor: Color(0xFF191919), statusBarBrightness: Brightness.light),
        ),
        cardTheme: const CardTheme(color: Color(0xFF111111), elevation: 0),
        textTheme: TextTheme(
          titleSmall: TextStyle(color: Colors.white.withOpacity(0.9), fontSize: 12, fontWeight: FontWeight.w300),
          titleMedium: TextStyle(color: Colors.white.withOpacity(0.9), fontSize: 15, fontWeight: FontWeight.w500), // TextFiled 默认文字样式
          titleLarge: TextStyle(color: Colors.white.withOpacity(0.9), fontSize: 18, fontWeight: FontWeight.bold), // AppBar默认文字样式
          bodySmall: TextStyle(color: Colors.white.withOpacity(0.7), fontSize: 12, fontWeight: FontWeight.w300),
          bodyMedium: TextStyle(color: Colors.white.withOpacity(0.7), fontSize: 15, fontWeight: FontWeight.w500), // Text默认文字样式
          bodyLarge: TextStyle(color: Colors.white.withOpacity(0.7), fontSize: 18, fontWeight: FontWeight.bold),
          displaySmall: TextStyle(color: Colors.white.withOpacity(0.5), fontSize: 12, fontWeight: FontWeight.w300),
          displayMedium: TextStyle(color: Colors.white.withOpacity(0.5), fontSize: 15, fontWeight: FontWeight.w500),
          displayLarge: TextStyle(color: Colors.white.withOpacity(0.5), fontSize: 18, fontWeight: FontWeight.bold), // Button child Text默认文字样式
        ),
        colorScheme: darkColorScheme,
      ),
      builder: _builder,
    );
  }
}

Widget _builder(BuildContext context, Widget? child) {
  return MediaQuery(
    ///设置字体不随系统字体变化
    data: MediaQuery.of(context).copyWith(textScaler: const TextScaler.linear(1.0)),
    child: child!,
  );
}